import string
import random

from django.contrib.auth.models import AbstractUser
from django.db import models

from company.models import Job


# Create your models here.


class User(AbstractUser):
    company = models.ForeignKey('Company', on_delete=models.CASCADE, null=True, blank=True, verbose_name="关联平台")
    username = models.CharField(max_length=255, unique=True, verbose_name="用户名")
    password = models.CharField(max_length=255, verbose_name="密码")
    email = models.EmailField(unique=True)
    avatar = models.URLField(null=True, blank=True, verbose_name="头像")
    role = models.SmallIntegerField(default=0, choices=((0, "求职者"), (1, "boss"), (2, "hr")),
                                    verbose_name="角色")

    class Meta:
        db_table = "user"
        verbose_name = "用户"
        verbose_name_plural = verbose_name


def generate_random_string():
    return ''.join(random.choices(string.ascii_letters + string.digits, k=16))


class Company(models.Model):
    name = models.CharField(max_length=255, unique=True, verbose_name="公司名称")
    logo = models.URLField(null=True, blank=True, verbose_name="公司logo")
    front_icon = models.URLField(null=True, blank=True, verbose_name="公司Icon")
    front_color = models.CharField(max_length=100, null=True, blank=True, verbose_name="前端颜色")
    company_profile = models.TextField(null=True, blank=True, verbose_name="公司简介")
    chat_id = models.CharField(max_length=100, unique=True, default=generate_random_string, verbose_name="公司ID")
    create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name="创办时间")
    start_time = models.TimeField(null=True, blank=True, verbose_name="上班时间", default="08:00:00")
    end_time = models.TimeField(null=True, blank=True, verbose_name="下班时间", default="17:30:00")
    job = models.ForeignKey(to=Job, on_delete=models.CASCADE, null=True, blank=True, verbose_name="关联工作",
                            related_name="company")

    class Meta:
        db_table = "company"
        verbose_name = "公司"
        verbose_name_plural = verbose_name
